<?php defined('BASEPATH') or die("No access");
    class Customer_model extends MY_Model
    {
        public function __construct()
        {
            parent::__construct();
        }
        //get all customer
        public function getAll($limit,$offset,$status)
        {
            $this->db->order_by('customer.customer_id','desc');
            $this->db->limit($limit,$offset);
            $this->db->from('customer');
            $this->db->join('city','city.city_id=customer.customer_city');
            $this->db->join('district','district.district_id=customer.customer_district');
            $query=$this->db->get();
            return $query->result(); 
        }
        //get customer by id 
        public function getCustomerById($id)
        {
            $this->db->where('customer_id',$id);
            $query=$this->db->get('customer');
            return $query->row();
        }
        
        //get customer by email & password
        public function getCustomerByUsernamePassword($username,$password)
        {
            $password=md5(md5(md5($password)));
            $this->db->where('customer_username',$username);
            $this->db->where('customer_password',$password);
            $query=$this->db->get('customer');
            return $query->row_array();
        }
        
        //check customer is exits
        public function checkCustomer($username,$email)
        {
            $this->db->where('customer_username',$username);
            $this->db->or_where('customer_email',$email);
            $query=$this->db->get('customer');
            $data=$query->result_array();
            if(!empty($data))
            {
                return true;
            }
            else
            {
                return false;
            }
            
        }
        
        // insert or update 
        public function save($id,$data=array())
        {
            if($id==0)
            {
                //insert new record
                if(!$this->checkCustomer($data['customer_username'],$data['customer_email']))
                {
                     $this->db->insert('customer',$data);
                     return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                $this->db->where('customer_id',$id);
                $this->db->update('customer',$data);
            }
        }
        //delete array customers
        public function delete($id=array())
        {
            $this->db->where_in('customer_id',$id);
            $this->db->delete('customer');
            return $this->db->affected_rows();
        }
        //publish or unpublish
        public function publish($status,$id=array())
        {
            $this->db->where_in('customer_id',$id);
            if($status==0)
            {
                $this->db->update('customer',array('customer_publish'=>0));
            }
            else
            {
                $this->db->update('customer',array('customer_publish'=>1));
            }
            return $this->db->affected_rows();
        }
    }
?>

